<?php
class Category_model extends Model {
	function __construct() {
		parent::Model();
	}
	
	function get_main() {
		$this->db->where('parent_id',0);
		$this->db->order_by('order','asc');
		$query = $this->db->get('categories');
		return $query->result_array();
	}
	function any() {
		$query = $this->db->get('categories');
		return $query->result_array();
	}
	function all() {
		$this->db->where('parent_id != ',0);
		$query = $this->db->get('categories');
		return $query->result_array();
	}
	function add($data) {
		$this->db->insert('categories',$data);
		return $this->db->insert_id();
	}
	function update($id,$data) {
		$this->db->where('id',$id);
		$this->db->update('categories',$data);
	}
	function identify($id) {
		$this->db->where('id',$id);
		$query = $this->db->get('categories');
		return $query->first_row('array');
	}
	function identify_by_name($name) {
		$this->db->where('name',$name);
		$query = $this->db->get('categories');
		return $query->first_row('array');
	}
	function child_name($parent_id,$name) {
		$this->db->where('parent_id',$parent_id);
		$this->db->where('name',$name);
		$query = $this->db->get('categories');
		return $query->first_row('array');
	}
	function delete($id) {
		$this->db->where('id',$id);
		$this->db->delete('categories');
	}
	function get($parent_id) {
		$this->db->where('parent_id',$parent_id);
		$this->db->order_by('order','asc');
		$query = $this->db->get('categories');
		return $query->result_array();
	}
	function get_previous($parent_id,$order) {
		$this->db->where('parent_id',$parent_id);
		$this->db->where('order <',$order);
		$this->db->order_by('order','desc');
		$query = $this->db->get('categories');
		return $query->first_row('array');
	}	
	function get_next($parent_id,$order) {
		$this->db->where('parent_id',$parent_id);
		$this->db->where('order >',$order);
		$this->db->order_by('order','asc');
		$query = $this->db->get('categories');
		return $query->first_row('array');
	}
	function remove_products($category_id) {
		$this->db->where('category_id',$category_id);
		$this->db->delete('products_categories');
	}
	
}
?>